package com.lyEducation.controllers.dto.system;

import com.lyEducation.entities.system.SysMenuEntity;
import lombok.Getter;
import lombok.Setter;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

@Getter
@Setter
public class SysMenuDto extends AbstractSysMenuDto {
    private TreeSet<? extends SysMenuDto> childMenus;
//    private List<SysRoleDto> roleList;


    protected SysMenuDto() {
    }

    public SysMenuDto(
            String id,
            String parentId,
            TreeSet<? extends SysMenuDto> childMenus,
//            List<SysRoleDto> roleList,
            String name,
            int status,
            String url,
            int level,
            int sort) {
        this.setId(id);
        this.setParentId(parentId);
        this.setChildMenus(childMenus);
//        this.roleList = roleList;
        this.setName(name);
        this.setStatus(status);
        this.setUrl(url);
        this.setLevel(level);
        this.setSort(sort);
    }

    public void setChildMenus(TreeSet<? extends SysMenuDto> childMenus) {
        if (childMenus == null) {
            childMenus =
                    new TreeSet<SysMenuDto>(
                            new Comparator<SysMenuDto>() {
                                @Override
                                public int compare(SysMenuDto o1, SysMenuDto o2) {
                                    if (o1.getId().equals(o2.getId())) {
                                        return 0;
                                    }
                                    int sort = o1.getSort() - o2.getSort();
                                    if (sort == 0) {
                                        return 1;
                                    }
                                    return sort;
                                }
                            });
        }
        this.childMenus = childMenus;
    }

    public static SysMenuDto createSysMenuDto(SysMenuEntity sysMenuEntity) {

        TreeSet<SysMenuEntity> childMenusSet = sysMenuEntity.getChildren();
        TreeSet<SysMenuDto> childenMenuDtoSet =
                new TreeSet<SysMenuDto>(
                        new Comparator<SysMenuDto>() {
                            @Override
                            public int compare(SysMenuDto o1, SysMenuDto o2) {
                                if (o1.getId().equals(o2.getId())) {
                                    return 0;
                                }
                                if (o1.getSort() - o2.getSort() == 0) {
                                    return 1;
                                }
                                return o1.getSort() - o2.getSort();
                            }
                        });
        if (childMenusSet != null && childMenusSet.size() != 0) {

            Iterator<SysMenuEntity> childMenusSetIterator = childMenusSet.iterator();
            while (childMenusSetIterator.hasNext()) {
                SysMenuEntity childMenu = childMenusSetIterator.next();
                childenMenuDtoSet.add(createSysMenuDto(childMenu));
            }
        }
//        List<SysRoleDto> sysRoleDtoList = new ArrayList<SysRoleDto>();
//        if (sysMenuEntity.getSysRoleList() != null && sysMenuEntity.getSysRoleList().size() > 0) {
//            for (SysRoleEntity sysRoleEntity : sysMenuEntity.getSysRoleList()) {
//                sysRoleDtoList.add(SysRoleDto.createSysRoleDtoBySysRoleEntity(sysRoleEntity));
//            }
//        }
        SysMenuDto sysMenuDto =
                new SysMenuDto(
                        sysMenuEntity.getId(),
                        sysMenuEntity.getParentId(),
                        childenMenuDtoSet,
//                        sysRoleDtoList,
                        sysMenuEntity.getName(),
                        sysMenuEntity.getStatus(),
                        sysMenuEntity.getUrl(),
                        sysMenuEntity.getLevel(),
                        sysMenuEntity.getSort());
        return sysMenuDto;
    }

    public static TreeSet<SysMenuDto> createSysMenuDtoSet(TreeSet<SysMenuEntity> menuSet) {
        TreeSet<SysMenuDto> sysMenuDtoSet =
                new TreeSet<SysMenuDto>(
                        new Comparator<SysMenuDto>() {
                            @Override
                            public int compare(SysMenuDto o1, SysMenuDto o2) {
                                if (o1.getId().equals(o2.getId())) {
                                    return 0;
                                }
                                if (o1.getSort() - o2.getSort() == 0) {
                                    return 1;
                                }
                                return o1.getSort() - o2.getSort();
                            }
                        });
        if (menuSet != null && menuSet.size() > 0) {
            Iterator<SysMenuEntity> sysMenuEntityIterator = menuSet.iterator();
            while (sysMenuEntityIterator.hasNext()) {
                SysMenuEntity sysMenuEntity = sysMenuEntityIterator.next();
                sysMenuDtoSet.add(SysMenuDto.createSysMenuDto(sysMenuEntity));
            }
        }
        return sysMenuDtoSet;
    }

    @Override
    public String toString() {
        return "SysMenuDto{}";
    }
}
